﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Web; 
using System.Web.UI;
using System.Web.UI.WebControls;
using WebMovil.Base;

namespace WebMovil
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (label1.Text == "0")
            {
                var db = new DataClasses1DataContext();
                var articulo = db.ArticulosXBodegas.OrderBy(a => a.IdMaestroXArticulo).First();
                label1.Text = articulo.IdMaestroXArticulo.ToString();
            }
        }
          

        public void GetArticulos()
        {
            var db = new DataClasses1DataContext();
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{

        //    string fileName = FileUpload1.FileName;
        //    byte[] fileByte = FileUpload1.FileBytes;
        //    Binary binaryObj = new Binary(fileByte);
        //    var db = new DataClasses1DataContext();
        //    db.MaestroArticulos.InsertOnSubmit(new MaestroArticulo { Comentario = "Prueba", IdCompañia = 1, Codigo = "11P", Foto = "11P", Picture = binaryObj });
        //    db.SubmitChanges();
        //}

        public void Inventario(int id)
        {
            var db = new DataClasses1DataContext();
            var inv = db.ArticulosXBodegas.Where(a => a.IdMaestroXArticulo == id);
            var n = db.MaestroArticulos.First(a => a.IdMaestroArticulo == id);
            if (!inv.Any())
                return;
            var stock = inv.Sum(a => a.Stock);
            var disponible = inv.Sum(a => a.Solicitado);
            var comprometido = inv.Sum(a=> a.Comprometido);
            var solicitado = inv.Sum(a => a.Solicitado);
            var articulo = new ArticuloVenta { Nombre =  n.Descripcion ,Codigo = n.Codigo, Comprometido =  comprometido, Disponible = disponible, Solicitado = solicitado, Stock = stock};
            IList<ArticuloVenta> list = new List<ArticuloVenta>(); 
            list.Add(articulo);
            GridView1.DataSource = list;
            GridView1.DataBind();
            id = id + 1;
            label1.Text = (id).ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var id = int.Parse(label1.Text);
            var db = new DataClasses1DataContext();
            var artic = (from a in db.MaestroArticulos where a.IdMaestroArticulo == id select a);
            if (artic.Any())
            {
                Image1.ImageUrl = "~/Handler1.ashx?Id=" + id;
                
            }
            
            Inventario(id);
        }
 

    }
}
